package com.pzhuweb.sgblog.handler.mybatisPlus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.pzhuweb.sgblog.domain.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 鄧浩奇
 * @classname MyMetaObjectHandler
 * @description 配置mybatisplus字段自动填充
 * @since 2023/7/7 15:38
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private Long userId = null;

    @Override
    public void insertFill(MetaObject metaObject) {
//        Long userId = null;
        try {
            userId = SecurityUtils.getUserID();

        } catch (ClassCastException e) {
            e.printStackTrace();
        }

        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("createBy", userId, metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", userId, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("", SecurityUtils.getUserID(), metaObject);
        this.setFieldValByName("updateBy", SecurityUtils.getUserID(), metaObject);
    }
}
